package com.wushi.lenovo.asia5b.app_ui.other.dialog.trans;

import android.view.View;

import com.mvp.base.model.ResultModel;
import com.mvp.utils.DecimalUtil;
import com.mvp.utils.StringUtils;
import com.mvp.utils.https.MapHelper;
import com.wushi.lenovo.asia5b.R;
import com.wushi.lenovo.asia5b.app_event.TransferEvent;
import com.wushi.lenovo.asia5b.app_model.credit.EmailModel;
import com.wushi.lenovo.asia5b.app_model.credit.MemberBlanceModel;
import com.wushi.lenovo.asia5b.app_model.other.TransItemBean;
import com.wushi.lenovo.asia5b.app_model.other.UserInfo;

import org.greenrobot.eventbus.EventBus;

import java.util.Map;

import io.reactivex.Observable;
import io.reactivex.functions.Consumer;

import static timber.log.Timber.d;

/**
 * Created by zhangyuncai on 2019/11/27.
 */
public class TransferDFragment extends PayDialogFragment {
    MemberBlanceModel memberBlanceModel;
    EmailModel emailModel;
    String transAcount;
    Map<String, Object> map;

    public void setData(MemberBlanceModel memberBlanceModel, EmailModel emailModel, String acountStr) {
        this.memberBlanceModel = memberBlanceModel;
        this.emailModel = emailModel;
        this.transAcount = acountStr;
        map = MapHelper.newHashMap();
        //member_id
        map.put("member_id", UserInfo.getMemberId());
        //transfer_to
        map.put("transfer_to", emailModel.getEmail());
        //payment_password
//        map.put("payment_password", "" + s);
        //amount
        map.put("amount", acountStr + "");
    }

    public void setMap(Map<String, Object> map) {
        this.map = map;
    }

    @Override
    protected void initView() {
        super.initView();
        setViewForTransfer();
    }

    public void setViewForTransfer() {
        mDatas.clear();
        mDatas.add(new TransItemBean(getString(R.string.c_transfer_receiver), emailModel.getName()));//姓名
        mDatas.add(new TransItemBean(getString(R.string.c_transfer_email), emailModel.getEmail()));//邮箱
        mDatas.add(new TransItemBean(getString(R.string.c_transfer_creditbalance), memberBlanceModel.getBalance_value()));//账户金额
        mDatas.add(new TransItemBean(getString(R.string.c_transfer_transferamount), StringUtils.formatNum2(transAcount)));//转账金额
        String totalBalance = DecimalUtil.subtract(memberBlanceModel.getBalance_value(), transAcount);
        d("--->>>>>>>>---totalBalance:" + totalBalance);
        mDatas.add(new TransItemBean(getString(R.string.c_transfer_totalbalance), StringUtils.formatNum2(totalBalance)));//转账后余额
        mAdapter.notifyDataSetChanged();
    }

    /**
     * 获取转账的接口
     * @param payPass
     * @return
     */
    @Override
    protected Observable<ResultModel> getSubmitObservable(String payPass) {
        map.put("payment_password", "" + payPass);
        return apiService.getUsernameTransferAccount(map);
    }

    /**
     * 转账成功
     * @return
     */
    @Override
    protected Consumer<ResultModel> getSubmitResult() {
        return resultModel -> {
            if (resultModel.isSuccess()) {
                EventBus.getDefault().post(new TransferEvent());
            }
            int successText = R.string.c_transfer_transfersuccessful;
            int errorText = R.string.c_transfer_transferfailed;
            ll_trans_notice_vessel.setVisibility(View.VISIBLE);
            if (resultModel.isSuccess()) {
                iv_trans_noticeicon.setImageResource(R.drawable.icon_trans_right);
                tv_trans_notice.setText(successText);
            } else {
                iv_trans_noticeicon.setImageResource(R.drawable.icon_trans_error);
                tv_trans_notice.setText(errorText);
            }
        };
    }
}
